package fy.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
public class apCheckVender {

	public static void main(String[] args) throws Exception  {
		// TODO Auto-generated method stub
		try {
			System.out.println(check("1","1","1"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
public static String check(String companyCode,String vendorName,String account) throws Exception, IOException  {

	String reResult="";
  	String wsdl = "http://192.168.231.165:8080/axis2/services/ApCheckVendor?wsdl";
		int timeout = 30000;
		StringBuffer sb = new StringBuffer("");
		sb.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:bfs=\"http://bfsErpService\">");
		sb.append("<soap:Header/>");
		sb.append("<soap:Body>");
		sb.append("<bfs:CheckVendor>");
		sb.append("<bfs:CompanyCode>"+companyCode+"</bfs:CompanyCode>");//ERP公司编码
		sb.append("<bfs:VendorName>"+vendorName+"</bfs:VendorName>");//目前ERP接口写死 暂时写死
		sb.append("<bfs:Account>"+account+"</bfs:Account>");
		sb.append("</bfs:CheckVendor>");
		sb.append("</soap:Body>");
		sb.append("</soap:Envelope>");

		// HttpClient发送SOAP请求
		System.out.println("HttpClient 发送SOAP请求" + sb);
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(wsdl);
		// 设置连接超时
		client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
		// 设置读取时间超时
		client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
		// 然后把Soap请求数据添加到PostMethod中
		RequestEntity requestEntity;
		try {
			requestEntity = new StringRequestEntity(sb.toString(), "application/soap+xml", "UTF-8");
			// 设置请求头部，否则可能会报 “no SOAPAction header” 的错误
			postMethod.setRequestHeader("SOAPAction", "");
			// 设置请求体
			postMethod.setRequestEntity(requestEntity);
			int status = client.executeMethod(postMethod);
			// 打印请求状态码
			System.out.println("status:" + status);
			// 获取响应体输入流
			InputStream is = postMethod.getResponseBodyAsStream();
			// 获取请求结果字符串
			String result = IOUtils.toString(is);
			System.out.println("result: " + result);
			
 SAXReader saxReader = new SAXReader();
			Document document= saxReader.read(new StringReader(result));
			DefaultXPath xpath = new DefaultXPath("//ns:return");
			xpath.setNamespaceURIs(Collections.singletonMap("ns", "http://bfsErpService"));
             List list = xpath.selectNodes(document);
			Iterator iterator = list.iterator();
			while (iterator.hasNext()) {
				Element node = (Element) iterator.next();
				reResult= node.getText();
			//	System.out.println("return: " + node.getText());
			}
			

  		} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		return reResult;
}
}
